<!-- 模板下载 -->
<template>
  <el-button type="primary" size="small" @click="downloadTemplate(templateId)">{{
    buttonText
  }}</el-button>
</template>

<script>
import { downloadLsfTemp } from '@/api/sys/template.js'
export default {
  name: 'downloadFile',
  props: {
    buttonText: {
      type: String,
      default: '下载打印模版',
    },
    templateId: {
      type: Number,
      default: 0,
    },
  },
  components: {},
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 下载模版
    async downloadTemplate() {
      const result = await downloadLsfTemp(this.templateId)
      if (result.data.code == 200) {
        // 触发下载
        var a = document.createElement('a') // 创建一个<a></a>标签
        a.href = window.baseURL.SERVICE_CONTEXT_PATH + 'lsf/' + result.data.data
        a.download = result.data.data // 设置下载文件文件名
        a.style.display = 'none' // 隐藏a标签
        document.body.appendChild(a) // 将a标签追加到文档对象中
        a.click() // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
      } else {
        this.$message.error(result.message)
      }
    },
  },
}
</script>
<style lang="less" scoped></style>
